package com.showcai.controller.order;

import com.github.pagehelper.PageInfo;
import com.showcai.controller.base.BaseController;
import com.showcai.def.Constant;
import com.showcai.module.ExpressInfo;
import com.showcai.module.Order;
import com.showcai.module.OrderItem;
import com.showcai.module.Payment;
import com.showcai.myRunTime.MyRunTimeExcption;
import com.showcai.service.*;
import com.showcai.tools.KdniaoSubscribeAPI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import java.util.List;
import java.util.Map;

/**
 * Created by Baggio on 2018-9-6.
 */
@RequestMapping("/order")
@Controller
public class OrderController extends BaseController {

    @Autowired
    OrderService orderService;
    @Autowired
    PaymentService paymentService;
    @Autowired
    ExpressInfoService expressInfoService;
    @Autowired
    OrderItemService orderItemService;
    @Autowired
    ProductEntityService productEntityService;
    @Autowired
    ApplyService applyWatchService;

    @RequestMapping(value = "/getOrderList")
    @ResponseBody
    public ModelAndView getOrderList(Integer page, Integer pageSize) {
        ModelAndView mov = new ModelAndView();
        if (page == null || page == 0) {
            page = Constant.PAGENUM;
        }
        // 设置每页记录数
        if (pageSize == null || pageSize == 0) {
            pageSize = Constant.PAGESIZENum;
        }
        Map map = super.showRequestParameters();
        map.put("page", String.valueOf(page));
        map.put("pageSize", String.valueOf(pageSize));
        PageInfo<Order> list = orderService.getOrderPageList(map);
        mov.addObject("pageinfo", list);
        mov.addObject("bean", map);
        mov.setViewName("/order/orderList");
        return mov;
    }

    @RequestMapping(value = "/getPaymentList")
    @ResponseBody
    public ModelAndView getPaymentList(Long orderId) {
        ModelAndView mov = new ModelAndView();
        List<Payment> list = paymentService.selectpaymentListByOrderId(orderId);
        mov.addObject("paymentlist", list);
        mov.setViewName("/order/payment");
        return mov;
    }

    @RequestMapping(value = "/getExpressInfoList")
    public ModelAndView getExpressInfoList(Long orderId) {
        ModelAndView mov = new ModelAndView(new MappingJackson2JsonView());
        try {
            ExpressInfo expressInfo = expressInfoService.selectExpressInfoByOrderId(orderId);
            String info = KdniaoSubscribeAPI.getOrderTracesByJson("ZTO", "280678686363");
            mov.addObject("expressInfo", info);
        } catch (MyRunTimeExcption myRunTimeExcption) {
            myRunTimeExcption.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        mov.setViewName("/order/expressInfoList");
        return mov;
    }

    @RequestMapping(value = "/getOrderItemList")
    public ModelAndView getOrderItemList(Long orderId) {
        ModelAndView mov = new ModelAndView();
        List<OrderItem> list = orderItemService.getOrderItemByOrderId(orderId);
        mov.addObject("orderItemList", list);
        mov.setViewName("/order/orderItemList");
        return mov;
    }
}
